package com.frxuexi.park2210.sign

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.databinding.ActivitySignBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask


@Route(path = "/app/SignActivity")
@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding,SignViewModel>() {
    lateinit var mapView: MapView
    lateinit var baiduMap: BaiduMap
    lateinit var mLocationClient:LocationClient
    lateinit var timer: Timer
    override fun initData() {
        vm.signSuc.observe(this){
            ToastUtils.showLong("打卡成功")
            Log.i("===",it)
        }
    }

    override fun initView() {
        initMap()
        timer=Timer()
        lifecycleScope.launch {
            timer.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.tvTime.setText(vm.getTime(System.currentTimeMillis()))
                }
            },0,1000)
        }



        val spUtils = SPUtils.getInstance()

        var startTime = spUtils.getString("sign", "")
        if(startTime.isNotEmpty()){
            if(startTime>"09:00:00"){
                startTime="$startTime(迟到)"
            }
            binding.signStartTime.setText(startTime)
            binding.tvSignText.setText("签退")
        }

        var endTime = spUtils.getString("sout", "")
        if(endTime.isNotEmpty()){
            if(endTime<"18:00:00"){
                endTime="$endTime(早退)"
            }
            binding.signEndTime.setText(endTime)
            binding.tvSignText.setText("签退")
            binding.btnSign.isEnabled=false
        }

        binding.btnSign.setOnClickListener {
            var time = vm.getTime(System.currentTimeMillis())
            if(binding.tvSignText.text.toString()=="签到"){
                binding.tvSignText.setText("签退")
                if(time>"09:00:00"){
                    time="$time(迟到)"
                }
                binding.signStartTime.setText(time)
                spUtils.put("sign",time)
            }else{
                binding.btnSign.isEnabled=false
                if(time<"18:00:00"){
                    time="$time(早退)"
                }
                binding.signEndTime.setText(time)
                spUtils.put("sout",time)
            }
            val map = mutableMapOf<String, Any>()
            map["startAddress"]="天津市"
            map["startTime"]=vm.getDateTime(System.currentTimeMillis()).replace(" ","T")+"Z"
            map["userId"]=spUtils.getInt(Const.UID)
            vm.sign(map)
        }
    }

    fun initMap(){
        mapView=binding.mapView
        baiduMap=mapView.map
        baiduMap.setMyLocationEnabled(true);

        LocationClient.setAgreePrivacy(true)
        //定位初始化
        //定位初始化
        mLocationClient = LocationClient(this)

//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)

//设置locationClientOption

//设置locationClientOption
        mLocationClient.setLocOption(option)

//注册LocationListener监听器

//注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient.registerLocationListener(myLocationListener)
//开启地图定位图层
//开启地图定位图层
        mLocationClient.start()
    }


    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mapView == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            //定位蓝点额外加的
            var ll = LatLng(location.latitude, location.longitude);
            val update = MapStatusUpdateFactory.newLatLngZoom(ll, 5f);
            baiduMap!!.animateMapStatus(update);
            baiduMap.setMyLocationData(locData)
        }
    }

    override fun onDestroy() {
        mLocationClient.stop()
        baiduMap.setMyLocationEnabled(false)
        mapView.onDestroy()
        super.onDestroy()
        timer.cancel()
    }

}